<?php

include './include/common.php';
if (! $_GET)
	exit ( '请不要直接访问此页面' );

if (! empty ( $_GET ['citypinyins'] )) {
	$citypinyin = $_GET ['citypinyins'];
} elseif (! empty ( $_GET ['citypinyint'] )) {
	$citypinyin = $_GET ['citypinyint'];
} elseif (! empty ( $_GET ['citypinyinf'] )) {
	$citypinyin = $_GET ['citypinyinf'];
} elseif (! empty ( $_GET ['citypinyint'] )) {
	$citypinyin = $_GET ['citypinyint'];
} elseif (! empty ( $_GET ['citypinyinm'] )) {
	$citypinyin = $_GET ['citypinyinm'];
} else {
	$citypinyin = $_GET ['citypinyin'];
}
if ($citypinyin == null)
	exit ( '参数不对' );
$Area = Getone_Area_Pinyin ( $citypinyin ); //地区详情
///如果是查看历史记录，则不用缓存，直接将信息呈现给用户。
if(isset($_GET['his']) && $_GET['his'] == 'list')		
{
	$page = isset($_GET['page']) ? $_GET['page'] : 1;
	
	if($cache = GetCache('list',$citypinyin.'_'.$page))
	{
		exit('s');
		echo $cache;
		
	}
	include './include/page.class.php';
	Add_Hot($Area ['id'],'hots');

	$WeatherHot = Select_Hot ( 'hot',8 ); //最近关注城市
	$WeatherHots = Select_Hot ( 'hots',15 ); //最近关注城市
	
	$WeatherHott = Select_Hot ( 'hott',8 ); //最近关注城市
	
	$WeatherHotf = Select_Hot ( 'hotf',8 ); //最近关注城市
	
	$WeatherHotm = Select_Hot ( 'hotm',24 ); //最近关注城市
	
	//$rs=$db->getone("select count(*) from {$table_top}weatherlog where `AreaID`='{$Area ['id']}'");
	
	
	if ($Area ['property'] == '2') {
		$parentArea = Getone_Area_Id ( $Area ['parentid'] );
		$ppArea = Getone_Area_Id ( $parentArea ['parentid'] );
	}
	elseif ($Area ['property'] == '1' && $Area ['Level'] == '3')
	{
		$parentArea = Getone_Area_Id ( $Area ['parentid'] );
		$ppArea = Getone_Area_Id ( $parentArea ['parentid'] );
	}
	else {
		$ppArea = Getone_Area_Id ( $Area ['parentid'] );
	}
	

		$areaid = $Area['id'];
	
	$stdate = mktime(1,1,1,1,1,1997);
	$count = 0;
	for($i=0; $i!=(date('Y',time())-1997)+1; ++$i)
	{
		$tab = getweathertable(date('y',strtotime("+{$i} year",$stdate)));
		$rs = $db->getone("select count(*) as c from {$tab} where `AreaID` = '{$areaid}' and Weather not like '%null' and Weather <> '' and live <> '' ");
		if($rs["c"] ==0)
		{
			$areaid = $Area['parentid'];
		}
		$rs=$db->getone("select count(*) as c from {$tab} where `AreaID`='{$areaid}' and Weather not like '%null%' and Weather <> '' and live <> '' ");
		$_c[date('Y',strtotime("+{$i} year",$stdate))] = $rs['c'];
		$count+=$rs["c"]; 
	}
	$numrows = $count;
	krsort($_c);			//逆向排序，按年份倒排
	$limit_end = $page*30;		//当前数据库结束的数据条数
	$my_code = array('c'=>0,	//已有数据库的总数据量
			't_1'=>null,		//第一个数据库的名称
			't_2'=>null,		//第二个数据库的名称
			't_1_limit'=>0,		//第一个数据库显示的条数
			't_2_limit'=>0,		//第二个数据库显示的条数
			'p_c' =>0,			//上一个数据库的总条数
			);
	foreach($_c as $k=>$v)
	{
		$my_code['c'] +=$v;
		if($limit_end <= $my_code['c'])		//如果结束的总条数小于当前数据库的数据条数
		{
			$my_code['t_1_limit'] = ( $limit_end-30 - $my_code['p_c']);			//确定第一个数据库开始显示的条数
			$my_code['t_1']	=	getweathertable(substr($k, 2,2));
			break;
		}else if($limit_end - $my_code['c'] < 30)				//如果处于第一个数据库与第二个数据库交接的地方
		{
			$my_code['t_1_limit'] = ($limit_end - 30 - $my_code['p_c']);			//确定第一个数据库开始显示的条数
			$my_code['t_1']	=	getweathertable(substr($k, 2,2));
			if($k != 1997)	//如果不是最后一个数据表
			{
				$my_code['t_2_limit'] = $limit_end - $my_code['c'];		//确定第二个数据库要显示的条数
				//next($_c);
				$my_code['t_2'] = getweathertable(substr(key($_c),2,2));										//第二个数据表的名称 
			}
			break;
		}
		$my_code['p_c']+=$v;
	}
		$h_tab = $my_code['t_1'];
		$h_start_1 = $my_code['t_1_limit'];
	//var_dump("select * from {$h_tab} where `AreaID` = '{$areaid}' and (`Update` between '{$sql_date_1}' and '{$sql_date_2}') and Weather not like '%null%' and Weather <> '' and live <> '' order by `Update` desc ");
		$his = $db->select("select * from {$h_tab} where `AreaID` = '{$areaid}' and Weather not like '%null%' and Weather <> '' and live <> '' order by `Update` desc limit {$h_start_1},30");
		if($my_code['t_2'] !=null)
		{
			$h_tab_2 = $my_code['t_2'];
			$h_end_2 = $my_code['t_2_limit'];
			$his_2 =  $db->select("select * from {$h_tab_2} where `AreaID` = '{$areaid}' and Weather not like '%null%' and Weather <> '' and live <> '' order by `Update` desc limit 0,{$h_end_2}");
			$his = array_merge($his,$his_2);
		}
		$h_page = new PageClass($numrows, 30, $page,"/history/{$citypinyin}/{page}/");
		$page_list = $h_page->myde_write();
		foreach($his as $v){
		$v['Weather'] = str_replace ( "@", "\\", $v['Weather'] );
		$v['Weather'] = json_decode ( $v['Weather'], TRUE );
		$urldate=substr($v['Update'],0,4).substr($v['Update'],5,2).substr($v['Update'],8,2);
		$v['Weather'][2]!=$v['Weather'][6]&&$tadd="转{$v['Weather'][6]}";
		$tt=$Area['Ab'].substr($v['Update'],0,4)."年".substr($v['Update'],5,2)."月".substr($v['Update'],8,2)."日天气预报(".$v['Weather'][2].$tadd.",最高温度".$v['Weather'][1]."℃,最低温度".$v['Weather'][5]."℃)";
		$hislist.="<a href=\"/history/{$citypinyin}/{$urldate}.html\">{$tt}</a><br>";
		}
		
		
		$title=$Area['Ab']."历史天气预报查询";
		$keywords = "{$Area[Ab]}历史天气预报";
		$description = "天气啦(www.tianqi.la)实时提供{$parentArea['Ab']}{$Area[Ab]}气象局播报的{$Area[Ab]}历史天气预报,包括提供{$parentArea['Ab']}{$Area[Ab]}地区各区县过去任意一天的温度,降水量,湿度,风向等超准天气预报查询!";
		if (! empty ( $parentArea )) {
		$daohang = "<a href=\"/\" >天气预报查询网</a> >  <a href=\"/history/\" >全国历史天气预报</a> > <a href=\"/history/{$parentArea[pinyin]}/\" >{$parentArea[Ab]}历史天气预报</a> > <a href=\"/history/{$Area[pinyin]}/\" >{$Area[name]}历史天气预报</a>";
		} else {
		if (empty ( $ppArea )) {
			$daohang = "<a href=\"/\" >天气预报查询网</a> >  <a href=\"/history/\" >全国历史天气预报</a> >  <a href=\"/history/{$Area[pinyin]}/\" >{$Area[name]}历史天气预报</a>";
		} else {
			$daohang = "<a href=\"/\" >天气预报查询网</a> >  <a href=\"/history/\" >全国历史天气预报</a> > <a href=\"/history/{$Area[pinyin]}/\" >{$Area[Ab]}历史天气预报</a>";
		}
	}
		ob_start();			
		include template('hislist');
		$cnt = ob_get_contents();
		ob_end_clean();
		//echo $cnt;
		var_dump(SetCache('list',$citypinyin.'_'.$page, $cnt,43200));
		exit;
}

if (strstr ( $SCRIPT_URL, '7tian' )) {
	echo getcache($citypinyin,"pages",7); 
}
else if(strstr ( $SCRIPT_URL, '10tian' ))
{
	echo getcache($citypinyin,"pages",10); 
}else if(strstr ( $SCRIPT_URL, '15tian' ))
{
	echo getcache($citypinyin,"pages",15); 
}else if(strstr ( $SCRIPT_URL, '30tian' ))
{
	echo getcache($citypinyin,"pages",30); 
}else
{
	echo getcache($citypinyin,"pages"); 
}